﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.A01.Entities;
using WebApplication1.A03.EntitiesProcess;
using WebApplication1.Models;

namespace WebApplication2.Controllers
{
    public class PersonController : Controller
    {
        private readonly IPersonRepository  _PersonService;//人员数据处理服务
        public PersonController(IPersonRepository service)
        {
            _PersonService = service;
        }
        // GET: Default
        ///<summary>
        ///数据列表，控制器缺省路由入口方法：
        ///负责根据传入的关键词，从存储中找出满足条件的对象，转成视图模型集合，与前端的视图Index中的
        
///模型@modelIEnumerable<PersonViewModel>绑定。
///</summary>
///<paramname="keyword">关键词</param>
///<returns>列表视图实例</returns>
public ActionResult Index(string keyword= null)
        {
            var persons = new List<Person>();
            if (String.IsNullOrEmpty(keyword))
                persons = _PersonService.GetPersons();
            else
                persons = _PersonService.GetPersons(keyword);
            var personVMCollection = new List<PersonViewModel>();
            var counter = 0;
            foreach (var item in persons)
            {
                var personVM = new PersonViewModel(item);
                personVM.OrderNumber = (++counter).ToString();
                personVMCollection.Add(personVM);
            }
            ViewData["Title"] = "员工数据维护管理";
            ViewData["Keyword"] = keyword;
            return View(personVMCollection);
        }
        // GET: Default/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: Default/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Default/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(IFormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: Default/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: Default/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, IFormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: Default/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: Default/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id, IFormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }
    }
}